<?php 
include_once "function_xml_basic.php";

function GetXmlVlanList()
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Type='vlan']";

	$ret_array = array();
	$results = GetAllNodeValue($query_string);
	$i = 0;
	foreach ($results as $result)
	{
		$ret_array[$i]['VlanId'] = $result->getAttribute('VlanId');
                $ret_array[$i]['Desc'] = $result->getAttribute('Desc');
                $ret_array[$i]['WorkModel'] = $result->getAttribute('WorkModel');
                $ret_array[$i]['Dev'] = $result->getAttribute('Dev');
                $ret_array[$i]['Protocol'] = $result->getAttribute('Protocol');
                $ret_array[$i]['Enabled'] = $result->getAttribute('Enabled');
		
		$query_string2 ="/MINI/NETWORK/INTERFACES/INTERFACE[@VlanId='". $ret_array[$i]['VlanId']. 
				"' and @Type='vlan' and @Dev='". $ret_array[$i]['Dev']. "']/ADDRESSES/ADDRESS";
	  
	        $ret_address = GetAllNodeValue($query_string2);
	        $k = 0;
	        foreach ($ret_address as $address)
	        {
	        	$ret_array[$i]['Ip'][$k] = $address->getAttribute('Ip');
	        	$ret_array[$i]['Mask'][$k] = $address->getAttribute('Mask');
	        	$k ++;
	        }
	 
                $i ++;
	}
	return $ret_array;
}
/*
$ret_array = GetXmlVlanList();
foreach ($ret_array as $ret)
{
	print $ret['Name'];
	print $ret['VlanId'];
}
*/

function GetXmlVlanNameByInfo()
{
	$ret_name_array = array ();
	$ret_array = GetXmlVlanList();
	$i = 0;
	foreach ($ret_array as $ret)
	{
		$ret_name_array[$i ++]  =  $ret['Dev']. '.'. $ret['VlanId'];
	}
	return $ret_name_array;
}
#$ret_list = GetXmlVlanNameByInfo();
#foreach ($ret_list as $ret)
#{
#	print $ret;
#}

function GetXmlSpecialVlanInfo($dev, $vlanid)
{
	$query_string = 
	"/MINI/NETWORK/INTERFACES/INTERFACE[@Dev='". $dev. "' and @VlanId='". $vlanid. "' and @Type='vlan']";
	$ret_array = array();
	$results = GetAllNodeValue($query_string);
	foreach ($results as $ret)
	{
		$ret_array['VlanId'] = $ret->getAttribute('VlanId'); 
		$ret_array['Desc'] = $ret->getAttribute('Desc'); 
		$ret_array['Dev'] = $ret->getAttribute('Dev'); 
		$ret_array['WorkModel'] = $ret->getAttribute('WorkModel'); 
		$ret_array['Protocol'] = $ret->getAttribute('Protocol'); 
		$ret_array['Enabled'] = $ret->getAttribute('Enabled'); 
		$ret_array['Enable_Ping'] = $ret->getAttribute('Enable_Ping'); 
		$ret_array['Enable_Trace'] = $ret->getAttribute('Enable_Trace'); 
		
		$query_string2 ="/MINI/NETWORK/INTERFACES/INTERFACE[@VlanId='". $vlanid. 
				"' and @Type='vlan' and @Dev='". $dev. "']/ADDRESSES/ADDRESS";
	  
	        $ret_address = GetAllNodeValue($query_string2);
	        $k = 0;
	        foreach ($ret_address as $address)
	        {
	        	$ret_array['Ip'][$k] = $address->getAttribute('Ip');
	        	$ret_array['Mask'][$k] = $address->getAttribute('Mask');
	        	$k ++;
	        }
	}
	return $ret_array;
}
#$ret = GetXmlSpecialVlanInfo('eth3', '4');
#print $ret['VlanId'];

function GetXmlSpecialVlanInfoByEth($dev)
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Dev='". $dev. "' and @Type='vlan']";
	$ret_array = array();
	$results = GetAllNodeValue($query_string);
	$i = 0;
	foreach ($results as $ret)
	{
		$ret_array[$i]['VlanId'] = $ret->getAttribute('VlanId'); 
		$ret_array[$i]['Desc'] = $ret->getAttribute('Desc'); 
		$ret_array[$i]['Dev'] = $ret->getAttribute('Dev'); 
		$ret_array[$i]['WorkModel'] = $ret->getAttribute('WorkModel'); 
		$ret_array[$i]['Protocol'] = $ret->getAttribute('Protocol'); 
		$ret_array[$i]['Enabled'] = $ret->getAttribute('Enabled'); 
		$ret_array[$i]['Enable_Ping'] = $ret->getAttribute('Enable_Ping'); 
		$ret_array[$i]['Enable_Trace'] = $ret->getAttribute('Enable_Trace'); 
		
		$query_string2 ="/MINI/NETWORK/INTERFACES/INTERFACE[@Type='vlan' and @Dev='". $dev. "']/ADDRESSES/ADDRESS";
	        $ret_address = GetAllNodeValue($query_string2);
	        $k = 0;
	        foreach ($ret_address as $address)
	        {
	        	$ret_array[$i]['Ip'][$k] = $address->getAttribute('Ip');
	        	$ret_array[$i]['Mask'][$k] = $address->getAttribute('Mask');
	        	$k ++;
	        }
		$i ++;
	}

	if ($i == 0)
		return FALSE;
	else
		return $ret_array;
}

function DelXmlNetDevOneVlan($dev, $vlanid)
{
	$query_string = 
	"/MINI/NETWORK/INTERFACES/INTERFACE[@Dev='". $dev. "' and @VlanId='". $vlanid. "' and @Type='vlan']";
	DelSpecialNode($query_string);
	ApplySysAllVlanDevInfo();
}

function DelXmlNetDevAllVlan()
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Type='vlan']";
	DelSpecialNode($query_string);
	FlushSysAllVlanConfig();
}

function AppendXmlNetDevOneVlan($Valu_array)
{
	$node_name = "INTERFACE";
	$query_string = "/MINI/NETWORK/INTERFACES";
	
	$Valu_array['Type'] = 'vlan';
	$temp_array ['Ip'] = $Valu_array['Ip'];
	$temp_array ['Mask'] = $Valu_array['Mask'];
	unset($Valu_array['Ip']);
	unset($Valu_array['Mask']);
	AppendAllAttrOfNode($query_string, $node_name, $Valu_array);

	$query_string .= "/". $node_name. "[last()]";
	$node_name = "ADDRESSES";
	$null_array = array ();
	AppendAllAttrOfNode($query_string, $node_name, $null_array);
	
	$query_string .= "/". $node_name;
	$node_name = "ADDRESS";
	AppendAllAttrOfNode($query_string, $node_name, $temp_array);

	ApplySysAllVlanDevInfo();
}

function EditXmlNetDevVlan($dev, $vlanid, $in_array)
{
	$query_string = 
	"/MINI/NETWORK/INTERFACES/INTERFACE[@Dev='". $dev. "' and @VlanId='". $vlanid. "' and @Type='vlan']";
	
	$in_array['Type'] = 'vlan';
	$temp_array ['Ip'] = $in_array['Ip'];
	$temp_array ['Mask'] = $in_array['Mask'];
	unset($in_array['Ip']);
	unset($in_array['Mask']);
	EditAllAttrOfNode($query_string, $in_array);

	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Dev='". 
			$dev. "' and @VlanId='". $vlanid. "' and @Type='vlan']/ADDRESSES/ADDRESS";
	EditAllAttrOfNode($query_string, $temp_array);
	
	ApplySysAllVlanDevInfo();
}

include_once "function_misc.php";

function GetVlanDevListFromSys()
{
	$command_ifconfig = "/sbin/ifconfig ";

	$command = $command_ifconfig. " -a | grep eth[0-9]| grep \"\.\" | awk '{print $1}' ";
	$ret_string = shell_exec($command);
	
	$ret_array = explode("\n", $ret_string);

        #foreach ($ret_array as $ret)
        #{
        #       print "[". $ret. "]\n";
        #}
        return $ret_array;
}

function ApplySysAllVlanDevInfo()
{
	$command = "";
	$command_vconfig= "/usr/local/sbin/vconfig ";
	$command_ifconfig = "/sbin/ifconfig ";
	
	FlushSysAllVlanConfig();
	
	$ret_v_list = GetXmlVlanList();	
	
	$command = $command_vconfig. " set_name_type DEV_PLUS_VID_NO_PAD; ";


	foreach($ret_v_list as $v)
	{
		//print_r ($v);
		$command .= $command_vconfig. " add ". $v['Dev']. " ". $v['VlanId']. " ;";
		$i = 0;
		foreach ($v['Ip'] as $ip){
			if ($ip != ""){
				$command .= $command_ifconfig. $v['Dev']. ".". $v['VlanId']. " ". $ip;
				if ($v['Mask'][$i] != ""){
					$command .="  netmask ". $v['Mask'][$i]. " ;";
				}
				else
					$command .= " ;";
				$i ++;
			}
		}
		$command .= $command_ifconfig. $v['Dev']. ".".$v['VlanId']. " up ; ";
	}
	//print $command."\n\n";
	$ret_string = shell_exec($command);
	return $ret_string;
}

function FlushSysAllVlanConfig()
{
	$ret_array_vlan_list = GetVlanDevListFromSys();

	$command = "";
	$command_vconfig = "/usr/local/sbin/vconfig ";
	$command_ifconfig = "/sbin/ifconfig ";

	foreach ($ret_array_vlan_list as $v)
	{
		if ($v == "")
			continue;
		
		$command .= $command_ifconfig. " ". $v. " 0 ;";
		$command .= $command_vconfig. " rem ". $v. " ; ";
	}
	//print $command. "\n\n";
	$ret_string = shell_exec($command);
	return $ret_string;
}

?>
